package com.study.cacheManager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.study.constant.CacheConstant;
import com.study.dao.entity.TUser;
import com.study.dao.mapper.TUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

/**
 * @author canace
 * @version 1.0
 * @description 用户信息缓存管理器
 * @date 2024/1/28 12:03
 */

@Component
@RequiredArgsConstructor
public class UserInfoCacheManager {

    private final TUserMapper userMapper;

    /**
     * 根据用户名获取用户信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    @Cacheable(cacheManager = CacheConstant.REDIS_CACHE_MANAGER, cacheNames = CacheConstant.USER_INFO_BY_USERNAME_NAME, key = "#username")
    public TUser getUserInfoByUsername(String username) {
        LambdaQueryWrapper<TUser> eq = Wrappers.lambdaQuery(TUser.class)
                .eq(TUser::getUsername, username);

        return userMapper.selectOne(eq);
    }

    /**
     * 根据用户名清空用户信息缓存
     *
     * @param username 用户名
     */
    @CacheEvict(cacheManager = CacheConstant.REDIS_CACHE_MANAGER, cacheNames = CacheConstant.USER_INFO_BY_USERNAME_NAME, key = "#username")
    public void delCacheUserInfoByUsername(String username) {

    }
}
